package com.tanhua.manager;

import cn.hutool.core.bean.BeanUtil;
import com.tanhua.domain.Admin;
import com.tanhua.domain.AdminVo;
import com.tanhua.interceptor.AdminHolder;
import com.tanhua.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/system")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping(name = "获取验证码",value = "/users/verification")
    public ResponseEntity verification(String uuid, HttpServletResponse response) throws Exception {


        return adminService.verification(uuid,response);

    }


    @PostMapping(name = "登录",value = "/users/login")
    public ResponseEntity login(@RequestBody Map<String,String> map) throws Exception {
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");
        return adminService.login(username,password,verificationCode,uuid);

    }

    @PostMapping(name = "登录信息验证",value = "/users/profile")
    public ResponseEntity login() throws Exception {
        Admin admin = AdminHolder.getAdmin();
        AdminVo adminVo = new AdminVo();
        BeanUtil.copyProperties(admin,adminVo);
        return ResponseEntity.ok(adminVo);
    }



    @PostMapping(name = "用户登出",value = "/users/logout")
    public ResponseEntity logout() throws Exception {

        return adminService.logout();
    }






}
